package com.tianchi.lexiang.background;

import java.util.HashMap;

import com.tianchi.lexiang.R;
import com.tianchi.lexiang.background.adapter.UpdateSongInfoAdapter;
import com.tianchi.lexiang.background.util.UDiskAddSongUtil;
import com.tianchi.lexiang.bean.Song;
import com.tianchi.lexiang.myinterface.OnQueryCompleteListener;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class UpdateSongDialog extends Activity implements OnClickListener, OnQueryCompleteListener {
	
	private static final String TAG = "UpdateSongDialog";
	private UDiskAddSongUtil uDiskAddSongUtil = null;
	
	private EditText songNameTxt;
	private EditText singerNameTxt;
	private EditText songNameFirxtspellTxt;
	private EditText songNameWordsTxt;
	private EditText volumeTxt;
	
	private Spinner songTypeSpinner;
	private Spinner languageSpinner;
	private Spinner originalTrackSpinner;
	
	private Button updateBtn;
	private Button backBtn;
	
	private ArrayAdapter<CharSequence> songTypeAdapter;
	private ArrayAdapter<CharSequence> languageAdapter;
	private ArrayAdapter<CharSequence> volumeAdapter;;
	
	private String songTypeString;
	private String languageString;
	private String volumeString;
	
	private int songId;
	private Song mSong;
	
	private HashMap<String, Integer> songTypeMap = new HashMap<String, Integer>();
	private HashMap<String, Integer> languageMap = new HashMap<String, Integer>();
	private HashMap<String, Integer> volumeMap = new HashMap<String, Integer>();
	private HashMap<String, String>  songTypeStringMap = new HashMap<String, String>();
	private HashMap<Integer, String>  languageTypeStringMap = new HashMap<Integer, String>();
	
	private int page;
	private String firstSpell;
	
	private void setAllMap(){
		Resources resources = this.getResources();
		String [] songTypeArr =resources.getStringArray(R.array.song_type);
	    for (int i = 0; i < songTypeArr.length; i++) {
	    	songTypeMap.put(songTypeArr[i], i);
	    	if(i > 0){
	    		Log.d(TAG, "[setAllMap]....RTP" + i +"====>songTypeArr[" + i + "]====>" + songTypeArr[i]);
				songTypeStringMap.put("RTP"+i, songTypeArr[i]);
			}
		}
	    
	    String [] languageArr = resources.getStringArray(R.array.language);
	    for (int i = 0; i < languageArr.length; i++) {
	    	languageMap.put(languageArr[i], i);
	    }
	    
	    String[] languageCategoryb = resources.getStringArray(R.array.language_category);
	    for(int i =0; i<languageCategoryb.length; i++){
	    	languageTypeStringMap.put(i, languageCategoryb[i]);
	    }
	    
	    String [] volumeArr = resources.getStringArray(R.array.volume);
	    for (int i = 0; i < volumeArr.length; i++) {
	    	volumeMap.put(volumeArr[i], i);
	    }
		
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		this.onNewIntent(getIntent());
		super.onCreate(savedInstanceState);
		setContentView(R.layout.update_song_dialog);
		initView();
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		songId = intent.getIntExtra(UpdateSongInfoAdapter.SONG_ID, 0);
		page = intent.getIntExtra(UpdateSongInfoAdapter.PAGE, 0);
		firstSpell = intent.getStringExtra(UpdateSongInfoAdapter.FIRST_SPELL);
		Log.d(TAG, "[onNewIntent].....songId===>" + songId);
		if(uDiskAddSongUtil == null){
			uDiskAddSongUtil = UDiskAddSongUtil.getInstance(UpdateSongDialog.this);
		}
		setAllMap();
		mSong = uDiskAddSongUtil.getSongInfoBySongId(songId);
	}
	
	private void initView(){
		songNameTxt = (EditText) findViewById(R.id.song_name_txt);
		songNameTxt.setText(mSong.songName);
		
		singerNameTxt = (EditText) findViewById(R.id.singer_name_txt);
		singerNameTxt.setText(mSong.singerName);
		
		songNameFirxtspellTxt = (EditText) findViewById(R.id.song_name_firxtspell_txt);
		songNameFirxtspellTxt.setText(mSong.firstSpell);
		
		songNameWordsTxt = (EditText) findViewById(R.id.song_name_words_txt);
		songNameWordsTxt.setText(mSong.nameLength + "");
		
		volumeTxt = (EditText) findViewById(R.id.volume_txt);
		volumeTxt.setText(mSong.leftArea + "");
		
		songTypeSpinner = (Spinner) findViewById(R.id.song_type_spinner);
		songTypeAdapter = ArrayAdapter.createFromResource(this, R.array.song_type, R.xml.spinner_item);
		songTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		songTypeSpinner.setAdapter(songTypeAdapter);
		String songType = songTypeStringMap.get(mSong.type);
		songTypeString = songType;
		Log.d(TAG, "[initView]....songType===>" + songType);
		int songTypeSetSelection = songTypeMap.get(songType);
		Log.d(TAG, "[initView]....songTypeSetSelection===>" + songTypeSetSelection);
		songTypeSpinner.setSelection(songTypeSetSelection);
		songTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				TextView tv = (TextView) view;
				tv.setTextSize(22.0f);
				songTypeString = songTypeAdapter.getItem(position).toString();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
		
		languageSpinner = (Spinner) findViewById(R.id.language_spinner);
		languageAdapter = ArrayAdapter.createFromResource(this, R.array.language, R.xml.spinner_item);
		languageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		languageSpinner.setAdapter(languageAdapter);
		String languageType = languageTypeStringMap.get(Integer.parseInt(mSong.language) - 1);
		languageString = languageType;
		Log.d(TAG, "[initView].....languageType===>" + languageType);
		int languageSelection = languageMap.get(languageType);
		Log.d(TAG, "[initView]....languageSelection===>" + languageSelection);
		languageSpinner.setSelection(languageSelection);
		languageSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				TextView tv = (TextView) view;
				tv.setTextSize(22.0f);
				languageString = languageAdapter.getItem(position).toString();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
		
		originalTrackSpinner = (Spinner) findViewById(R.id.original_track_spinner);
		volumeAdapter = ArrayAdapter.createFromResource(this, R.array.volume, R.xml.spinner_item);
		volumeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		originalTrackSpinner.setAdapter(volumeAdapter);
		volumeString =  mSong.mtvOrVcd;
		Log.d(TAG, "[initView]...mSong.mtvOrVcd===> " + mSong.mtvOrVcd);
		int volumeSelection = volumeMap.get(mSong.mtvOrVcd);
		Log.d(TAG, "[initView].....volumeSelection===>" + volumeSelection);
		originalTrackSpinner.setSelection(volumeSelection);
		originalTrackSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				TextView tv = (TextView) view;
				tv.setTextSize(22.0f);
				volumeString = volumeAdapter.getItem(position).toString();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
		updateBtn = (Button) findViewById(R.id.update_btn);
		updateBtn.setOnClickListener(this);
		
		backBtn = (Button) findViewById(R.id.back_btn);
		backBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.update_btn:
			if (songNameTxt.getText().toString() == null || "".equals(songNameTxt.getText().toString())
				|| singerNameTxt.getText().toString() == null || "".equals(singerNameTxt.getText().toString())
				|| songNameFirxtspellTxt.getText().toString() == null || "".equals(songNameFirxtspellTxt.getText().toString())
				|| Integer.parseInt(songNameWordsTxt.getText().toString()) < 0 || "".equals(songNameWordsTxt.getText().toString())
				|| volumeTxt.getText().toString() == null || "".equals(volumeTxt.getText().toString())
			    || Integer.parseInt(volumeTxt.getText().toString()) < 0 || Integer.parseInt(volumeTxt.getText().toString()) > 100
		     	|| songTypeSpinner.getSelectedItemPosition() == 0 || languageSpinner.getSelectedItemPosition() == 0
			    || originalTrackSpinner.getSelectedItemPosition() == 0) {
				
					updateToast(getString(R.string.please_input_song_info));
					
			}else{
				mSong.songName = songNameTxt.getText().toString();
				mSong.singerName = singerNameTxt.getText().toString();
				mSong.firstSpell = songNameFirxtspellTxt.getText().toString();
				mSong.nameLength = Integer.parseInt(songNameWordsTxt.getText().toString());
				mSong.leftArea = Integer.parseInt(volumeTxt.getText().toString());
				mSong.rightArea = Integer.parseInt(volumeTxt.getText().toString());
				mSong.type = songTypeString;
				mSong.language = languageString;
				mSong.mtvOrVcd = volumeString;
				if(uDiskAddSongUtil == null){
					uDiskAddSongUtil = UDiskAddSongUtil.getInstance(UpdateSongDialog.this); 
				}
				if(uDiskAddSongUtil.updateSongInfoBySongId(mSong)){
					updateToast(getString(R.string.update_success));
					UpdateSongDialog.this.finish();
					UpdateSongInfo.mAdapter.startQueryAlreadyPonit(page, firstSpell);
				}
				else{
					updateToast(getString(R.string.update_failure));
				}
			}
			break;
		case R.id.back_btn:
				UpdateSongDialog.this.finish();
			break;

		default:
			break;
		}
		
	}

	
	private void updateToast(String tip){
		Toast.makeText(UpdateSongDialog.this, tip, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onQueryComplete(int totalCount) {
		
	}
}
